package nuc.soft.xsf.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import nuc.soft.xsf.model.Title;
import nuc.soft.xsf.service.ITitleService;
import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/TitleController", produces = "text/html;charset=UTF-8")
public class TitleController {
	@Resource
	private ITitleService titleService;

	@RequestMapping(value = "/add.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addTitle(Title title) {
		title.setDeletestate(0);
		titleService.insert(title);
		JSONObject json = new JSONObject();
		json.put("data", title);
		return json.toString();

	}

	@RequestMapping(value = "/delete.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String deleteTitle(String ids) {
		String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			Title title = titleService.findById(Integer.parseInt(strTmp));
			if (title != null) {
				title.setDeletestate(1);
				titleService.update(title);
				success++;
			}
		}
		json.put("success", success);
		return json.toString();
	}

	@RequestMapping(value = "/update.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String updateTitle(Title title) {
		titleService.update(title);
		System.out.println(title.toString());
		JSONObject json = new JSONObject();
		Title title1 = titleService.findById(title.getTitleId());
		json.put("data", title1);
		json.put("redata", title.getTitleName());
		json.put("error_code", 200);
		return json.toString();
	}

	@RequestMapping(value = "/findByCondition.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String findByCondition(Title title) {
		Map map = Tools.transBean2Map(title);
		List<Title> list = titleService.findByCondition(map);
		JSONObject json = new JSONObject();
		json.put("record", list);
		return json.getString("record");
	}
	
	
	@RequestMapping(value = "/getAllTitleInfo.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String getAllTitleInfo(Title title) {
		Map map = Tools.transBean2Map(title);
		List<Title> list = titleService.getAllTitleInfo(map);
		JSONObject json = new JSONObject();
		json.put("record", list);
		return json.getString("record");
	}
	
}
